﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace _06.FindConnectedComponents
{
    class Graph
    {
        private List<int>[] childNodes;

        public Graph(List<int>[] childNodes)
        {
            this.childNodes = childNodes;
        }

        public int NodesCount
        {
            get { return childNodes.Length; }
        }

        public void FindComponents()
        {
            bool[] visited = new bool[childNodes.Length];

            int componentsCount = 0;
            for (int i = 0; i < childNodes.Length; i++)
            {
                if (!visited[i])
                {
                    componentsCount++;
                    Console.WriteLine("Component: {0}", componentsCount);
                    RecursiveDFS(i, visited);
                }
            }
        }

        private void RecursiveDFS(int node, bool[] visited)
        {
            visited[node] = true;
            Console.WriteLine("Visited node {0}", node);
            foreach (var childNode in this.childNodes[node])
            {
                if (!visited[childNode])
                {
                    RecursiveDFS(childNode, visited);
                }
            }
        }
    }
}
